package com.cgsd.heyu.ui.common;


import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.android.library.utils.BeanUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.PhotoPagerAdapter;
import com.cgsd.heyu.entity.PhotoEntity;
import com.cgsd.heyu.presenter.BasePresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.utils.StatusBarUtil;
import com.cgsd.heyu.widgets.HackyViewPager;

import java.util.ArrayList;

/**
 * @author xiaoxing
 * Created by pengsheng-2 on 2018/5/15.
 */

public class PhotoActivity extends BaseActivity<BasePresenter> implements ViewPager.OnPageChangeListener {
    private TextView tvSum;
    private TextView tvPosition;
    private ArrayList<PhotoEntity> photos = new ArrayList<>();
    private RelativeLayout layoutBottom;

    public static void actionStart(Activity activity, ArrayList<PhotoEntity> photos, int position){
        Intent intent = new Intent(activity,PhotoActivity.class);
        intent.putParcelableArrayListExtra("obj",photos);
        intent.putExtra("position",position);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_photo;
    }

    @Override
    protected void initView() {
        photos =  getIntent().getParcelableArrayListExtra("obj");
        int position = getIntent().getIntExtra("position", 0);

        if (BeanUtils.isEmpty(photos)){
            return;
        }

        tvSum = findViewById(R.id.sum_pager);
        tvPosition = findViewById(R.id.position_pager);
        layoutBottom = findViewById(R.id.layout_bottom);
        HackyViewPager viewPager = findViewById(R.id.viewpager);

        PhotoPagerAdapter photoPagerAdapter = new PhotoPagerAdapter(this, photos);
        viewPager.setAdapter(photoPagerAdapter);
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(position);

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.fullBar(this);
    }

    @Override
    protected void initData() {
        tvSum.setText(String.valueOf(photos.size()));
        if (BeanUtils.isEmpty(photos) || photos.size() <= 1){
            layoutBottom.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvPosition.setText(String.valueOf(position+1));
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
